﻿#region Documentation
/*
 * Definition for the Payment entity
 *  
 * Copyright (C) 2014  Daniel Hamacher
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 * 
 * */
#endregion

using SpendingMonitor.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SpendingMonitor.ViewModels
{
    class PaymentViewModel : ViewModelBase
    {

        #region Properties
        private int id = 0;
        public int Id
        {
            get
            { return id; }

            set
            {
                if (id == value)
                { return; }

                id = value;
                RaisePropertyChanged("Id");
            }
        }

        private string category = string.Empty;
        public string Category
        {
            get
            { return category; }

            set
            {
                if (category == value)
                { return; }

                category = value;
                isDirty = true;
                RaisePropertyChanged("Category");
            }
        }

        private string retailer = string.Empty;
        public string Retailer
        {
            get
            { return retailer; }

            set
            {
                if (retailer == value)
                { return; }

                retailer = value;
                isDirty = true;
                RaisePropertyChanged("Retailer");
            }
        }

        private string paymentMethod = string.Empty;
        public string PaymentMethod
        {
            get
            { return paymentMethod; }

            set
            {
                if (paymentMethod == value)
                { return; }

                paymentMethod = value;
                isDirty = true;
                RaisePropertyChanged("PaymentMethod");
            }
        }       

        private double price = 0.00;
        public double Price
        {
            get
            { return price; }

            set
            {
                if (price == value)
                { return; }

                price = value;
                isDirty = true;
                RaisePropertyChanged("Price");
            }
        }

        private DateTime payedOn = System.DateTime.Today;
        public DateTime PayedOn
        {
            get
            { return payedOn; }

            set
            {
                if (payedOn == value)
                { return; }

                payedOn = value;
                isDirty = true;
                RaisePropertyChanged("PayedOn");
            }
        }

        private bool isDirty = false;
        public bool IsDirty
        {
            get
            {
                return isDirty;
            }

            set
            {
                isDirty = value;
                RaisePropertyChanged("IsDirty");
            }
        }
        #endregion "Properties"

        #region CRUD
        public PaymentViewModel GetEntry(int entryId)
        {
            var entry = new PaymentViewModel();
            using (var db = new SQLite.SQLiteConnection(SpendingMonitor.App.DBPath))
            {
                var _entry = (db.Table<Payment>().Where(
                    c => c.Id == entryId)).Single();
                entry.Id = _entry.Id;
                entry.Category = _entry.Category;
                entry.Retailer = _entry.Retailer;
                entry.PaymentMethod = _entry.PaymentMethod;
                entry.Price = _entry.Price;
                entry.PayedOn = _entry.PayedOn;
            }
            return entry;
        } 
        
        public string SaveEntry(PaymentViewModel entry)
        {
            string result = string.Empty;
            using (var db = new SQLite.SQLiteConnection(SpendingMonitor.App.DBPath))
            {             
                        int success = db.Insert(new Payment()
                        {
                            Category = entry.Category,
                            Retailer = entry.Retailer    
                        });                        
                        result = "Success";
            }    
            return result;
        }

        #endregion
    }       
}
